package com.ruoyi.qsng.service.impl;

import com.ruoyi.common.core.domain.AjaxResults;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.SnowFlakeUtil;
import com.ruoyi.qsng.domain.KActivity;
import com.ruoyi.qsng.domain.KActivityPurchase;
import com.ruoyi.qsng.mapper.KActivityMapper;
import com.ruoyi.qsng.mapper.KActivityPurchaseMapper;
import com.ruoyi.qsng.service.IKActivityPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 活动报名Service业务层处理
 *
 * @author kanglou
 * @date 2023-06-07
 */
@Service
public class KActivityPurchaseServiceImpl implements IKActivityPurchaseService {
    @Autowired
    private KActivityPurchaseMapper kActivityPurchaseMapper;

    @Autowired
    private KActivityMapper kActivityMapper;

    /**
     * 查询活动报名
     *
     * @param id 活动报名主键
     * @return 活动报名
     */
    @Override
    public KActivityPurchase selectKActivityPurchaseById(String id) {
        return kActivityPurchaseMapper.selectKActivityPurchaseById(id);
    }

    /**
     * 查询活动报名列表
     *
     * @param kActivityPurchase 活动报名
     * @return 活动报名
     */
    @Override
    public List<KActivityPurchase> selectKActivityPurchaseList(KActivityPurchase kActivityPurchase) {
        return kActivityPurchaseMapper.selectKActivityPurchaseList(kActivityPurchase);
    }

    /**
     * 新增活动报名
     *
     * @param kActivityPurchase 活动报名
     * @return 结果
     */
    @Override
    public AjaxResults insertKActivityPurchaseForApp(KActivityPurchase kActivityPurchase) {
        //新增报名活动判断 是否还有名额
        KActivity kActivity = kActivityMapper.selectKActivityById(kActivityPurchase.getActivityId());
        Assert.notNull(kActivity, "未查到该活动！");

        if ((kActivity.getTotalQuota() - kActivity.getSold()) <= 0) {
            return AjaxResults.error("报名人数已满！");
        }

        //判断时间
        if (!isEffectiveDate(new Date(), kActivity.getBmKssj(), kActivity.getBmJssj())) {
            return AjaxResults.error("该活动不在报名时间内！");
        }

        //更新人数
        kActivity.setSold(kActivity.getSold() + 1);
        int i = kActivityMapper.updateKActivity(kActivity);
        if (i == 0) {
            return AjaxResults.error("更新报名人数失败！");
        }

        kActivityPurchase.setId(SnowFlakeUtil.getNextId());
        kActivityPurchase.setCreateTime(DateUtils.getNowDate());
        return AjaxResults.success(kActivityPurchaseMapper.insertKActivityPurchase(kActivityPurchase));
    }


    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意三个参数的时间格式要一致
     *
     * @param nowTime
     * @param startTime
     * @param endTime
     * @return 在时间段内返回true，不在返回false
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        return date.after(begin) && date.before(end);
    }

    /**
     * 新增活动报名
     *
     * @param kActivityPurchase 活动报名
     * @return 结果
     */
    @Override
    public int insertKActivityPurchase(KActivityPurchase kActivityPurchase) {
        kActivityPurchase.setId(SnowFlakeUtil.getNextId());
        kActivityPurchase.setCreateTime(DateUtils.getNowDate());
        return kActivityPurchaseMapper.insertKActivityPurchase(kActivityPurchase);
    }

    /**
     * 修改活动报名
     *
     * @param kActivityPurchase 活动报名
     * @return 结果
     */
    @Override
    public int updateKActivityPurchase(KActivityPurchase kActivityPurchase) {
        kActivityPurchase.setUpdateTime(DateUtils.getNowDate());
        return kActivityPurchaseMapper.updateKActivityPurchase(kActivityPurchase);
    }

    /**
     * 批量删除活动报名
     *
     * @param ids 需要删除的活动报名主键
     * @return 结果
     */
    @Override
    public int deleteKActivityPurchaseByIds(String[] ids) {
        return kActivityPurchaseMapper.deleteKActivityPurchaseByIds(ids);
    }

    /**
     * 删除活动报名信息
     *
     * @param id 活动报名主键
     * @return 结果
     */
    @Override
    public int deleteKActivityPurchaseById(String id) {
        return kActivityPurchaseMapper.deleteKActivityPurchaseById(id);
    }
}
